package de.roskenet.clou.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.HttpRequestHandler;

import de.roskenet.clou.test.PageImport;

/**
 * Servlet implementation class ShowPage
 */
public class ShowPage implements HttpRequestHandler {

	private PageImport pageImport;
	
	public PageImport getPageImport() {
		return pageImport;
	}

	public void setPageImport(PageImport pageImport) {
		this.pageImport = pageImport;
	}

	@Override
	public void handleRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String pageName = request.getParameter("name");
		
		if(pageName == null) {
			pageName="test";
		}
		
		response.setContentType(pageImport.getMimeType(pageName));
		byte[] fileBytes = null;
		fileBytes = pageImport.getPage(pageName); 
	    ServletOutputStream os = response.getOutputStream();
	    os.write(fileBytes);
	    os.flush();
	    os.close();
		
	}
}
